﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Data;
using System.Data.SqlClient;
using FL.Entity.DB;
using FL.Web.BackEnd.Helper;
using FL.DAL;
using System.Collections;
namespace FL.Web.BackEnd.Controllers.Report
{
    public class ReportController : Controller
    {
        //
        // GET: /Hr/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 打印核算明细 add by zengtongchun 2015-09-18
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintAccountDetails()
        {
            Receivables receivables = new Receivables();
            var useNo = Request["useNo"];
            var useNo1 = Request["useNo1"];
            var periodName = Request["periodName"];
            string condition = string.IsNullOrEmpty(Request["condition"]) ? "" : Request["condition"];
            DataTable dt = receivables.GetData(condition);//核算明细
            string titleStr = "华商联" + receivables.GetTitleStr(condition) + "核算明细表";
           
            titleStr = titleStr+"    " + useNo + "#" + useNo1 + "#" + periodName;
            DataTable dt2 = receivables.GetDataCount(condition);//核算明细汇总
            DataSet ds = new DataSet();
            DataTable dt1 = new DataTable();
            dt1 = dt.Clone();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object[] arr = dt.Rows[i].ItemArray;
                dt1.Rows.Add(arr);
            }
            DataTable dt2Clone = new DataTable();
            dt2Clone = dt2.Clone();
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                object[] arr = dt2.Rows[i].ItemArray;
                dt2Clone.Rows.Add(arr);
            }

            dt2Clone.TableName = "subCount";
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2Clone);
            string title = "<_grparam><title>" + titleStr + "</title></_grparam>\r\n";
            return new ContentResult
            {
                Content = "<report>\r\n" + XmlReportData.GenDetailData(ds) + title + "\r\n</report>"
               // Content = XmlReportData.GenDetailData(ds)
            };

        }

        //打印客户快递单信息
        public ActionResult Print()
        {
            var id = Request["id"];
            string msql = string.Format(@"select * from t_fin_customer_express where id='{0}'", id);
            DataSet ReportDataSet = new System.Data.DataSet();
            //获取客户快递单信息
            DataTable empressDt = new DbSql().ExecuteQuery(msql);
            DataTable dt = new DataTable();
            dt = empressDt.Clone();
            for (int i = 0; i < empressDt.Rows.Count; i++)
            {
                object[] arr = empressDt.Rows[i].ItemArray;
                dt.Rows.Add(arr);
            }
            return new ContentResult
            {

                Content = XmlReportData.GenDetailData(dt)
            };

        }
        //客户联财务联双打印 add by zengtongchun 2016-02-19
        public ActionResult PrintCustomerAndFin()
        {
            Reconciliation reconciliation = new Reconciliation();
            //对账单id
            string id = string.IsNullOrEmpty(Request["id"]) ? "0" : Request["id"];
            //模板类型
            string tmpType = string.IsNullOrEmpty(Request["tmpType"]) ? "v_fin_CustomerExportTmp" : Request["tmpType"];
            //对账单编号
            string billNo = string.IsNullOrEmpty(Request["billNo"]) ? "0" : Request["billNo"];
            //模板id
            var tmpId = string.IsNullOrEmpty(Request["tmpId"]) ? "0" : Request["tmpId"];
            var tmpIds = tmpId.Split(',');
            string ids = string.IsNullOrEmpty(Request["printIds"]) ? "" : Request["printIds"];
            ids = ids.Replace("|", "'");
            //对账单打印不加对账查询session条件
            if (billNo!="0")
            {
                ids = null;
            }
            string sql = reconciliation.GetTmpSqlStr(tmpIds[0], id, billNo, tmpType, ids);//客户联
            string sql1 = reconciliation.GetTmpSqlStr(tmpIds[1], id, billNo, tmpType, ids);//财务联
            DataSet ReportDataSet = new DataSet();
            //获取打印资源
            DataTable empressDt = new DbSql().ExecuteQuery(sql);
            DataTable dt = new DataTable();
            DataTable empressDt1 = new DbSql().ExecuteQuery(sql1);
            DataTable dt1 = new DataTable();
            dt = empressDt.Clone();
            for (int i = 0; i < empressDt.Rows.Count; i++)
            {
                object[] arr = empressDt.Rows[i].ItemArray;
                dt.Rows.Add(arr);
            }
            dt.TableName = "PrintCustomer";

            dt1 = empressDt1.Clone();
            for (int i = 0; i < empressDt1.Rows.Count; i++)
            {
                object[] arr = empressDt1.Rows[i].ItemArray;
                dt1.Rows.Add(arr);
            }
            dt1.TableName = "PrintFinance";
            ReportDataSet.Tables.Add(dt);
            ReportDataSet.Tables.Add(dt1);
            return new ContentResult
            {

                Content = XmlReportData.FilterZero(ReportDataSet)
            };

        }

        public ActionResult printCustomer()
        {
            Reconciliation reconciliation = new Reconciliation();
            //对账单id
            string id = string.IsNullOrEmpty(Request["id"]) ? "0" : Request["id"];
            //模板类型
            string tmpType = string.IsNullOrEmpty(Request["tmpType"]) ? "v_fin_CustomerExportTmp" : Request["tmpType"];
            //对账单编号
            string billNo = string.IsNullOrEmpty(Request["billNo"]) ? "0" : Request["billNo"];
            //模板id
            var tmpId = string.IsNullOrEmpty(Request["tmpId"]) ? "0" : Request["tmpId"];

            string ids = string.IsNullOrEmpty(Request["printIds"]) ? "" : Request["printIds"];
            ids = ids.Replace("|", "'");
            //对账单打印不加对账查询session条件
            if (billNo != "0")
            {
                ids = null;
            }
            string sql = reconciliation.GetTmpSqlStr(tmpId, id, billNo, tmpType, ids);
            DataSet ReportDataSet = new System.Data.DataSet();
            //获取打印资源
            DataTable empressDt = new DbSql().ExecuteQuery(sql);
            DataTable dt = new DataTable();
            dt = empressDt.Clone();
            for (int i = 0; i < empressDt.Rows.Count; i++)
            {
                object[] arr = empressDt.Rows[i].ItemArray;
                dt.Rows.Add(arr);
            }
            //dt = FilterZero(dt);
            return new ContentResult
            {
                Content = XmlReportData.FilterZero(dt)
            };
        }
        /// <summary>
        /// 过滤0 add by zengtongchun 2016-07-30
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public DataTable FilterZero(DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string cell = dt.Rows[i][j].ToString();
                    
                        double b = 0;
                        if (double.TryParse(cell,out b))
                        {
                            int cell1 = Convert.ToInt32(cell);
                            if (cell1==0)
                            {
                                dt.Rows[i][j] = 0;
                            }
                            
                        }
                        
                        
                    
                }
            }
            return dt;
        }
    }
}

